// 这个文件打包文件

import minimist from "minimist";
import {resolve,dirname} from "path"
import {fileURLToPath} from "url"
import { createRequire } from "module";
import esbuild from "esbuild"

// node 中的命令参数通过 process  获取process.argv
const args = minimist(process.argv.slice(2))
const __filename = fileURLToPath(import.meta.url) //获取文件绝对路径
const __dirname = dirname(__filename)
const require = createRequire(import.meta.url)
const target = args._[0] || "reactivity" // 打包哪个项目
const format = args.f || 'life' //  打包后的模块化规范
console.log(__dirname)
// es模块中没有__dirname

const entry = resolve(__dirname,`../packages/${target}/src/index.ts`)
const pkg = require(`../packages/${target}/package.json`)
console.log(typeof entry)

esbuild.context({
    entryPoints:[entry],
    outfile:resolve(__dirname,`../packages/${target}/dist/${target}.js`),
    bundle:true,
    platform:"browser",
    sourcemap:true,
    format,
    globalName:pkg.buildOptions?.name
}).then((ctx)=>{
    console.log('start dev')
    return ctx.watch() // 监控入口文件持续进行打包处理
})
